package com.iiiss.ssh.impl.services;

import java.util.List;
import java.util.NoSuchElementException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.iiiss.ssh.core.daos.ColumnDao;
import com.iiiss.ssh.core.entities.Column;
import com.iiiss.ssh.core.services.ColumnService;

@Service
public class ColumnServiceImpl implements ColumnService {

	@Autowired
	private ColumnDao columnDao;

	@Transactional
	public Column add(String name, Long parentId) {
		if (name == null)
			throw new IllegalArgumentException("Argument 'name' is not specified.");
		if (parentId != null) {
			Column parent = columnDao.selectOne(parentId);
			if (parent == null)
				throw new NoSuchElementException("Column " + parentId + " does not exist.");
			else
				return columnDao.insert(name, parent);
		} else {
			return columnDao.insert(name);
		}
	}

	public List<Column> inquireAll() {
		return columnDao.selectAll();
	}

}
